<template>
	<view>
		<view class="content0" v-for="item,index in conversationList" :key="index" @click="toChat(item)">
			<view class="content">
				<view class="lef">
					<!-- item.unreadCount 未读数 -->
					<view class="img">
						<image :src="item.userProfile.avatar ? item.userProfile.avatar : ''" mode="aspectFill" />
						<view class="unread" v-if="item.unreadCount>0">
							{{item.unreadCount}}
						</view>
					</view>
					<view class="info">
						<p>{{ item.userProfile.nick ? item.userProfile.nick : '' }}</p>
						<view class="conts">{{ item.lastMessage ? item.lastMessage.messageForShow : '' }}</view>
					</view>
				</view>
				<view class="rig">{{ item.lastMessage ? getTimestamp(item.lastMessage.lastTime) : '' }}</view>
			</view>
		</view>
	</view>
</template>
<script>
	import TencentCloudChat from '@tencentcloud/chat';
	import {
		formatTime
	} from '@/utils/util.js'
	export default {
		name: "messageList",
		data() {
			return {
				conversationList: []
			};
		},
		mounted() {
			getApp().globalData.chat.on(TencentCloudChat.EVENT.CONVERSATION_LIST_UPDATED, this.getList)
			this.getList()
		},
		methods: {
			getList() {
				getApp().globalData.chat.getConversationList({
					type: TencentCloudChat.TYPES.CONV_C2C
				}).then(res => {
					this.conversationList = res.data.conversationList
					console.log(this.conversationList)
				}).catch(function(imError) {
					// 获取会话列表失败的相关信息
					console.warn('getConversationList error:', imError);
					getApp().IMLogin()
				});
			},
			getTimestamp(time) {
				return formatTime(time, 'MM-DD')
			},
			toChat(item) {
				console.log(item)
				uni.navigateTo({
					url: `/subpages/message/chat?conversationID=${item.conversationID}`
				})
			}
		}
	}
</script>

<style lang="scss">
	.content0 {
		.content {
			display: flex;
			justify-content: space-between;
			background: #ffffff;
			padding: 20rpx 30rpx;
			margin: 20rpx;
			border-radius: 25rpx;

			.lef {
				display: flex;
				align-items: center;

				.img {
					position: relative;

					image {
						width: 80rpx;
						height: 80rpx;
						margin-right: 20rpx;
						border-radius: 50%;
					}

					.unread {
						position: absolute;
						right: 10rpx;
						top: -10rpx;
						background: #FF4D4D;
						color: #fff;
						border-radius: 50%;
						padding: 0px 8rpx;
						/* height: 30rpx; */
						text-align: center;
						line-height: 30rpx;
						font-size: 28rpx;
					}
				}

				.info {
					.conts {
						//超过宽度用...显示
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
						width: 400rpx;
						font-size: 26rpx;
						color: #999999;
						margin-top: 10rpx;
					}
				}
			}

			.rig {
				font-size: 26rpx;
				color: #999999;
			}
		}
	}
</style>